package com.huanghuai.retail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanghuai.retail.common.R;
import com.huanghuai.retail.entity.Device;
import com.huanghuai.retail.mapper.DeviceMapper;
import com.huanghuai.retail.service.DeviceService;
import com.huanghuai.retail.service.DeviceTypeService;
import com.huanghuai.retail.service.ISailPlaceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
    @Resource
    private DeviceTypeService deviceTypeService;
    @Resource
    private ISailPlaceService sailPlaceService;

    @Override
    public List<Device> findByDeviceTypeId(Long id) {
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id!=null,Device::getDeviceTypeId, id);
        List<Device> devices = this.list(wrapper);
        if (devices != null && devices.size() > 0) {
            return devices;
        }
        return null;
    }

    @Override
    public R<String> saves(Device device) {
        if(device == null){
            return R.error("设备信息不能为空");
        }
        if(device.getDeviceTypeId()==null||
                device.getDeviceName()==null||
                device.getSailPlaceId() == null||
                device.getSoftwareVersion()==null||
                device.getDeviceNumber()==null||
                device.getStatus() == null
        ){
            return R.error("字段不能为空");
        }
        if(deviceTypeService.getById(device.getDeviceTypeId())==null){
            return R.error("设备类型不存在");
        }
        if (sailPlaceService.getById(device.getSailPlaceId()) == null) {
            return R.error("销售地点不存在");
        }
        save(device);
        return R.success("添加设备成功");
    }

    @Override
    public R<String> updates(Device device) {
        if(device==null){
            return R.error("设备信息不能为空");
        }
        if(device.getDeviceTypeId()==null||
                device.getDeviceName()==null||
                device.getSailPlaceId() == null||
                device.getSoftwareVersion()==null||
                device.getDeviceNumber()==null||
                device.getStatus() == null||
                device.getId()==null
        ){
            return R.error("字段不能为空");
        }
        if(deviceTypeService.getById(device.getDeviceTypeId())==null){
            return R.error("设备类型不存在");
        }
        if (sailPlaceService.getById(device.getSailPlaceId()) == null) {
            return R.error("销售地点不存在");
        }
        if(updateById(device)){
            return R.success("修改设备成功");
        }
        return R.error("修改设备失败");

    }

    @Override
    public R<Device> getByIds(Long id) {
        if(id == null||id<=0) {
            return R.error("id 不合理");
        }
        return null;
    }

}
